package com.cty.util;

import org.springframework.util.DigestUtils;

public class MD5Utils {
    /*固定的盐值*/
    private static final String SALT = "1a2b3c4d";

    public static void main(String[] args) {
        String password = "abcd1234";
        System.out.println(formPassToDBPass(password,"1a2b3c4d"));
        System.out.println(formPassToDBPass(password,"1a2b3c4d"));
    }
    public static String md5(String src){
        return DigestUtils.md5DigestAsHex(src.getBytes());//md5Hex(src);
    }

    /**
     * 第一次md5加密
     * 用于通过输入的密码生成传输的密码：方法通过固定盐值和明文密码之间拼接再生成md5
     * @return
     */
    public  static String inputPassToFormPass(String password){//c3zhangsan0901b2
        String str = ""+SALT.charAt(5) + SALT.charAt(4) + password + SALT.charAt(3) + SALT.charAt(2);
        return md5(str);  //c3zhangsan0901b2 md5 加密
    }

    /**
     * 第二次md5加密：通过输入的密码和数据库随机盐值，继续生成密码
     */
    public  static String formPassToDBPass(String password,String salt){
        String str = "" + salt.charAt(0) + salt.charAt(2) + password+ salt.charAt(5) + salt.charAt(4);
        return md5(str);
    }

    public static  String inputPassToDBPass(String password,String dbSalt){
        String  firstMd5 = inputPassToFormPass(password);
        String seconfMd5 = formPassToDBPass(firstMd5,dbSalt);
        return  seconfMd5;
    }
}
